import fs from 'fs';

export function getTodayWeather(html){
    const regex_info = /<dl\s+class="weather_info">[\s\S]*?<dl>/gmi
    fs.writeFileSync('./index.html', html)
    const match = regex_info.exec(html);


    const jsonData = {
        date: '', // 日期
        tempera_today: '', // 今日天气
        temperature_now: '', // 当前天气
        humidity: '', // 湿度
        wind_direction: '',
        ultraviolet_rays: '',
        air_quality: '',
        pm: '',
        sunrise: '',
        sunset: '',
    };

    if(match[0]) {
        const weather_info = match[0];
        const regex_now = /<p\s+class="now"><b>(\d*?)<\/b>/gmi
        const regex_week = /<dd\s+class="week">([\s\S]*?)<\/dd>/gmi
        const now = regex_now.exec(weather_info);
        const week = regex_week.exec(weather_info);
        console.log("date: ", week[1]);
        console.log("temp: ", now[1], '℃');

        const regex_day = /<span><b>(.+)<\/b>(\d+.+\d+).+<\/span>/gmi
        const day = regex_day.exec(weather_info);
        console.log("day: ", day[1]);
        console.log("range: ", day[2], '℃');

        const regex_shidu = /<dd\s+class="shidu"><b>湿度：(\d*?)%<\/b><b>风向：(.*?)<\/b><b>紫外线：(.*?)<\/b>/gmi
        const shidu = regex_shidu.exec(weather_info);
        console.log("湿度: ", shidu[1], '%');
        console.log("风向: ", shidu[2]);
        console.log("紫外线: ", shidu[3]);

        const regex_kongqi = /<dd\s+class="kongqi"\s+>(.*)空气质量：(.+)<\/h5><h6>PM:\s*(\d+)<\/h6><span>日出:(.*)<br \/>日落: (.*?)<\/span>/gmi
        const kongqi = regex_kongqi.exec(weather_info);
        console.log("空气质量: ", kongqi[2]);
        console.log("PM: ", kongqi[3]);
        console.log("日出: ", kongqi[4]);
        console.log("日落: ", kongqi[5]);

        jsonData.date = week[1];
        jsonData.tempera_today = day[2];
        jsonData.temperature_now = now[1];
        jsonData.humidity = shidu[1];
        jsonData.wind_direction = shidu[2];
        jsonData.ultraviolet_rays = shidu[3];
        jsonData.air_quality = kongqi[2];
        jsonData.pm = kongqi[3];
        jsonData.sunrise = kongqi[4];
        jsonData.sunset = kongqi[5];

        const body = `
            <div>
                <div>${week[1]}</div>
                <div class="info">
                    <div>今日温度: ${day[2]}℃</div>
                    <div>当前温度: ${now[1]}℃</div>
                    <div>湿度: ${shidu[1]}%</div>
                    <div>风向: ${shidu[2]}</div>
                    <div>紫外线: ${shidu[3]}</div>
                    <div>空气质量: ${kongqi[2]}</div>
                    <div>PM: ${kongqi[3]}</div>
                    <div><span>日出: ${kongqi[4]}</span> <span>日落: ${kongqi[5]}</span></div>
                </div>
            </div>
        `

        const outputHTML = `<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Weather</title>
    <style>
        .info{margin-left: 15px;margin-top: 10px;}
    
    </style>
</head>
<body>
${body}
</body>
</html>`;
        // output html
        fs.writeFileSync('./weather.html', outputHTML);

        // output json
        fs.writeFileSync('./weather.json', JSON.stringify(jsonData));
    }
}

export function getTodayHoursWeather(html){
    const regex = /<div class="twty_hour">\w*<dl>/gm
    const match = regex.exec(html);
    console.log(match[1]);
}